package com.example.aihub.util;

import com.example.aihub.dto.CommentDTO;
import com.example.aihub.dto.PostDTO;
import com.example.aihub.dto.UserDTO;
import com.example.aihub.entity.Comment;
import com.example.aihub.entity.Post;
import com.example.aihub.entity.User;

public class ModelMapper {

    // 将User实体转换为UserDTO
    public static UserDTO mapUserToDTO(User user) {
        UserDTO userDTO = new UserDTO();
        userDTO.setId(user.getId());
        userDTO.setUsername(user.getUsername());
        userDTO.setEmail(user.getEmail());
        userDTO.setBio(user.getBio());
        userDTO.setAvatar(user.getAvatar());
        userDTO.setCreatedAt(user.getCreatedAt());
        return userDTO;
    }

    // 将Post实体转换为PostDTO
    public static PostDTO mapPostToDTO(Post post) {
        PostDTO postDTO = new PostDTO();
        postDTO.setId(post.getId());
        postDTO.setTitle(post.getTitle());
        postDTO.setContent(post.getContent());
        postDTO.setUser(mapUserToDTO(post.getUser()));
        postDTO.setViewCount(post.getViewCount());
        postDTO.setLikeCount(post.getLikeCount());
        postDTO.setCommentCount(post.getCommentCount());
        postDTO.setCreatedAt(post.getCreatedAt());
        postDTO.setStatus(post.getStatus().name());
        // 添加视频字段映射
        postDTO.setVideoUrl(post.getVideoUrl());
        postDTO.setVideoThumbnailUrl(post.getVideoThumbnailUrl());
        // 添加封面图片URL映射
        postDTO.setCoverImageUrl(post.getCoverImageUrl());
        // 添加分类字段映射
        postDTO.setCategory(post.getCategory());
        return postDTO;
    }

    // 将Comment实体转换为CommentDTO
    public static CommentDTO mapCommentToDTO(Comment comment) {
        CommentDTO commentDTO = new CommentDTO();
        commentDTO.setId(comment.getId());
        commentDTO.setContent(comment.getContent());
        commentDTO.setUser(mapUserToDTO(comment.getUser()));
        commentDTO.setPostId(comment.getPost().getId());
        if (comment.getParentComment() != null) {
            commentDTO.setParentCommentId(comment.getParentComment().getId());
        }
        commentDTO.setCreatedAt(comment.getCreatedAt());
        return commentDTO;
    }
}